package com.ssh.demo.user.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssh.demo.common.util.ExtAjaxResponse;
import com.ssh.demo.common.util.ExtPageable;
import com.ssh.demo.user.entity.User;
import com.ssh.demo.user.entity.dto.UserQueryDTO;
import com.ssh.demo.user.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired	private IUserService userService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody String saveOrUpdate(User user) 
	{
		try {
			userService.saveOrUpdate(user);
			return "success";
		} catch (Exception e) {
			return "error";
		}
		
	}
	
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{
		try {	
			User user =  userService.findOne(id);
			if(null!=user) {
				userService.delete(user);
			}
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}
	
	@RequestMapping("/deleteUsers")
	public @ResponseBody ExtAjaxResponse deleteUsers(@RequestParam Long[] ids) 
	{
		try {	
			List<Long> idLists = Arrays.asList(ids);
			if(null!=idLists) {
				userService.delete(idLists);
			}
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}
	
	@RequestMapping("/findOne")
	public @ResponseBody User findOne(@RequestParam Long id) 
	{
		User user =  userService.findOne(id);
		return user;
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<User> findAll() 
	{
		List<User> lists =  userService.findAll();	
		return lists;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<User> findPage(UserQueryDTO userQueryDTO, ExtPageable pageable) {
		Page<User> page = userService.findAll(UserQueryDTO.getSpecification(userQueryDTO), pageable.getPageable());
		return page;
	}

}
